# SPDX-License-Identifier: GPL-3.0-or-later
cmake_minimum_required(VERSION 3.28)
project(slingshot_animals_recomp LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_SCAN_FOR_MODULES OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
    set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Build type" FORCE)
endif()

# The user supplies the original APKs; nothing from them is part of this repository.
set(GAME_APKS "" CACHE STRING "APKs of the supported games (games/*/game.json), separated by ;")
set(GAME_EXTRA_starwars2 "" CACHE PATH "Star Wars II's files/assets_service/ of an installation, as zip or folder (optional)")
set(GAME_ROOT "${CMAKE_BINARY_DIR}/games" CACHE PATH "Where the APKs' libraries, assets and icons are extracted (<root>/<game id>)")
option(SLINGSHOT_MODS "Build the mods in mods/ into the executables" ON)
set(SLINGSHOT_DEFAULT_MODS "all" CACHE STRING
    "Mods enabled when a game is started without --mod or SLINGSHOT_MODS: all, none, or a list such as hide-dead-services;offline-shop")

include(cmake/Dependencies.cmake)
if(SLINGSHOT_HOST_TOOLS)
    foreach(tool abrecomp slingshot-apk)
        add_executable(${tool} IMPORTED GLOBAL)
        set_target_properties(${tool} PROPERTIES IMPORTED_LOCATION "${SLINGSHOT_HOST_TOOLS}/${tool}${CMAKE_HOST_EXECUTABLE_SUFFIX}")
    endforeach()
else()
    add_subdirectory(tools/recompiler)
    add_subdirectory(tools/apk)
endif()
if(SLINGSHOT_TOOLS_ONLY)
    return()
endif()

# Match each APK to its game by the library (games/<id>/game.json): the game's
# APK is GAME_APK_<id>. games/ extracts it at build time (slingshot-apk).
file(GLOB game_files CONFIGURE_DEPENDS "${CMAKE_SOURCE_DIR}/games/*/game.json")
foreach(apk IN LISTS GAME_APKS)
    file(TIMESTAMP "${apk}" stamp "%s" UTC)
    string(MD5 key "${apk}${stamp}")
    if(NOT DEFINED SLINGSHOT_APK_MATCH_${key})
        if(NOT EXISTS "${apk}")
            message(FATAL_ERROR "No such APK: ${apk}")
        endif()
        set(tmp "${CMAKE_BINARY_DIR}/apk-match")
        file(REMOVE_RECURSE "${tmp}")
        file(ARCHIVE_EXTRACT INPUT "${apk}" DESTINATION "${tmp}" PATTERNS "lib/armeabi-v7a/*.so")
        set(match "")
        foreach(f IN LISTS game_files)
            file(READ "${f}" json)
            string(JSON lib GET "${json}" library)
            string(JSON sha GET "${json}" sha256)
            string(JSON version GET "${json}" version)
            set(so "${tmp}/lib/armeabi-v7a/lib${lib}.so")
            if(EXISTS "${so}")
                get_filename_component(d "${f}" DIRECTORY)
                get_filename_component(id "${d}" NAME)
                file(SHA256 "${so}" digest)
                if(NOT digest STREQUAL sha)
                    message(FATAL_ERROR "${apk}: unsupported build of ${id} (library sha256 ${digest}), needs ${version}")
                endif()
                set(match "${id}")
            endif()
        endforeach()
        file(REMOVE_RECURSE "${tmp}")
        if(match STREQUAL "")
            message(FATAL_ERROR "${apk}: no supported game library")
        endif()
        set(SLINGSHOT_APK_MATCH_${key} "${match}" CACHE INTERNAL "")
    endif()
    set(GAME_APK_${SLINGSHOT_APK_MATCH_${key}} "${apk}")
endforeach()

add_subdirectory(runtime)
add_subdirectory(platform/sdl)
if(SLINGSHOT_MODS)
    add_subdirectory(mods)
endif()
add_subdirectory(games)
